﻿using System;
using System.ComponentModel.DataAnnotations;

namespace OpenVision.Models
{
    public class User
    {
        private int id;
        private string nickname;
        private string password;
        private string email;
        private string sessionKey;
        private double latitude;
        private double longitude;
        
        public User()
        {
        }

        public User(int id, string nickname, string password, string email)
        {
            this.Id = id;
            this.Nickname = nickname;
            this.Password = password;
            this.Email = email;
        }

        public int Id
        {
            get
            {
                return this.id;
            }

            set
            {
                if (value < 0)
                {
                    throw new ArgumentOutOfRangeException("Invalid id! It must be a positive integer value!");
                }

                this.id = value;
            }
        }

        [MinLength(5, ErrorMessage = "Invalid nickname! It must contain more than 5 characters!")]
        public string Nickname
        {
            get
            {
                return this.nickname;
            }

            set
            {
                ValidateStringField(value, "nickname");

                this.nickname = value;
            }
        }

        [MinLength(8, ErrorMessage = "Invalid password! It must contain more than 8 characters!")]
        public string Password
        {
            get
            {
                return this.password;
            }

            set
            {
                ValidateStringField(value, "password");

                this.password = value;
            }
        }

        public string Email
        {
            get
            {
                return this.email;
            }

            set
            {
                ValidateStringField(value, "email");

                this.email = value;
            }
        }

        public string SessionKey
        {
            get
            {
                return this.sessionKey;
            }

            set
            {
                this.sessionKey = value;
            }
        }

        public double Latitude
        {
            get
            {
                return this.latitude;
            }
            set
            {
                this.latitude = value;
            }
        }

        public double Longitude
        {
            get
            {
                return this.longitude;
            }
            set
            {
                this.longitude = value;
            }
        }

        public int Range { get; set; }

        public byte[] SoundMessage { get; set; }

        private void ValidateStringField(string value, string field)
        {
            if (string.IsNullOrWhiteSpace(value) || value == string.Empty)
            {
                string message = string.Format("Invalid {0}! It must be an existing non-empty string that contains not only white spaces!",
                    field);
                throw new ArgumentException(message, field);
            }
        }
    }
}